package walletadminlogic

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryWithdrawFlowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryWithdrawFlowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryWithdrawFlowLogic {
	return &QueryWithdrawFlowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询提现流水
func (l *QueryWithdrawFlowLogic) QueryWithdrawFlow(in *pb.QueryWithdrawFlowReq) (*pb.QueryWithdrawFlowResp, error) {
	defer utils.RecoverPanic()

	var wf *wallet.UserWithdrawFlow
	var err error
	wf, err = l.svcCtx.UserWithdrawFlowModel.FindOne(l.ctx, in.FlowNo)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		if err == wallet.ErrNotFound {
			err = xerr.NewGrpcErrCodeMsg(xerr.WalletFlowNotFound, err.Error())
		}
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var resp pb.QueryWithdrawFlowResp
	_ = copier.Copy(&resp, wf)
	//
	resp.AccountName = wf.AccountName
	resp.BankCardNumber = wf.BankCardNumber
	resp.BankName = wf.BankName
	resp.BankCode = wf.BankCode
	resp.AccountType = wf.AccountType
	resp.CurrencyAmount = decimal.NewFromInt(wf.CurrencyAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
	return &resp, nil
}
